/**
 * 
 */
package com.etc.param.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.etc.common.exception.DataOperationException;
import com.etc.common.exception.SysOperationException;
import com.etc.common.util.CustomBeanUtils;
import com.etc.param.dao.IParamMapper;
import com.etc.param.model.ParamModel;
import com.etc.param.service.IParamService;
import com.etc.param.vo.ParamVO;
import com.github.pagehelper.PageHelper;

/**
 * @author Administrator
 *
 */
@Service("paramService")
public class ParamServiceImpl implements IParamService {
	
	@Autowired
	private IParamMapper paramMapper;

	/* (non-Javadoc)
	 * @see com.etc.common.service.IBaseService#insert(com.etc.common.model.BaseModel)
	 */
	@Override
	public Integer insert(ParamVO vo) {
		try {
			ParamModel model = new ParamModel();
			CustomBeanUtils.copyProperties(model, vo);
			return this.paramMapper.insert(model);
		} catch (Exception e) {
			String errMsg = String.format("param保存异常,参数 :vo=%s", vo);
			throw new DataOperationException(errMsg, e);
		}
	}

	/* (non-Javadoc)
	 * @see com.etc.common.service.IBaseService#delete(java.io.Serializable)
	 */
	@Override
	public void delete(Integer id) {
		try {
			this.paramMapper.delete(id);
		} catch (Exception e) {
			String errMsg = String.format("param删除异常,参数 :id=%s", id);
			throw new DataOperationException(errMsg, e);
		}

	}

	/* (non-Javadoc)
	 * @see com.etc.common.service.IBaseService#update(com.etc.common.model.BaseModel)
	 */
	@Override
	public void update(ParamVO vo) {
		try {
			ParamModel model = new ParamModel();
			CustomBeanUtils.copyProperties(model, vo);
			this.paramMapper.update(model);
		} catch (Exception e) {
			String errMsg = String.format("param更新异常,参数 :vo=%s", vo);
			throw new DataOperationException(errMsg, e);
		}

	}

	/* (non-Javadoc)
	 * @see com.etc.common.service.IBaseService#selectById(java.io.Serializable)
	 */
	@Override
	public ParamVO selectById(Integer id) {
		ParamVO vo = new ParamVO();
		try {
			ParamModel model = this.paramMapper.selectById(id);
			CustomBeanUtils.copyProperties(vo, model);
			return vo;
		} catch (Exception e) {
			String errMsg = String.format("param查询异常,参数:id=%s", id);
			throw new SysOperationException(errMsg,e);
		}
	}

	/* (non-Javadoc)
	 * @see com.etc.common.service.IBaseService#list(java.util.Map)
	 */
	@Override
	public List<ParamVO> list(Map<String, Object> param) {
		try{
			PageHelper.startPage((Integer) param.get("pageNo"), (Integer) param.get("pageNum"));
			List<ParamModel> modelList = this.paramMapper.list(param);
			List<ParamVO> voList = new ArrayList<ParamVO>();
	
			for (ParamModel model : modelList) {
				ParamVO vo = new ParamVO();
				CustomBeanUtils.copyProperties(vo, model);
				voList.add(vo);
			}
			return voList;
		} catch (Exception e) {
			String errMsg = String.format("param查询异常,参数:param=%s", param);
			throw new SysOperationException(errMsg,e);
		}
	}

	/* (non-Javadoc)
	 * @see com.etc.common.service.IBaseService#count(java.util.Map)
	 */
	@Override
	public int count(Map<String, Object> param) {
		return paramMapper.count(param);
	}

}
